package com.jjlin.leetcode;

import java.util.HashMap;
import java.util.Map;

public class Solution3 {
    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int len = s.length();
        int res = 0;
        for(int start = 0, end = 0; end < len; end++){
            char ch = s.charAt(end);
            if(map.containsKey(ch)){
                start = Math.max(start, map.get(ch));//更新start
            }
            res = Math.max(res, end - start + 1);//更新最大窗口长度
            map.put(ch, end + 1);//更新hashMap
        }
        return res;
    }
}
